package fr.lip6.meta.ple.plextraction;
//import java.util.ArrayList;
import java.util.Iterator;
//import java.util.List;

import fr.lip6.meta.tools.*;

import org.eclipse.emf.common.util.EList;
//import org.eclipse.emf.common.util.TreeIterator;
//import org.eclipse.emf.ecore.EAttribute;
//import org.eclipse.emf.ecore.EObject;
//import org.eclipse.emf.ecore.impl.DynamicEObjectImpl;
//import org.eclipse.emf.ecore.impl.EAttributeImpl;
//import org.eclipse.emf.ecore.impl.EClassImpl;
//import org.eclipse.emf.ecore.impl.EModelElementImpl;
//import org.eclipse.emf.ecore.impl.ENamedElementImpl;
//import org.eclipse.emf.ecore.impl.EObjectImpl;
//import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.gmt.modisco.java.*;
import org.eclipse.gmt.modisco.java.Package;
import org.eclipse.gmt.modisco.java.emf.impl.AssignmentImpl;
//import org.eclipse.gmt.modisco.java.emf.impl.ClassDeclarationImpl;
import org.eclipse.gmt.modisco.java.emf.impl.ExpressionStatementImpl;
import org.eclipse.gmt.modisco.java.emf.impl.FieldAccessImpl;
import org.eclipse.gmt.modisco.java.emf.impl.FieldDeclarationImpl;
import org.eclipse.gmt.modisco.java.emf.impl.PackageImpl;
import org.eclipse.gmt.modisco.java.emf.impl.SingleVariableAccessImpl;
//import org.eclipse.gmt.modisco.java.emf.impl.StatementImpl;

//import sun.org.mozilla.javascript.internal.ast.Block;

//import artefact.umlClassDiagram.ClassDiagram;
//import artefact.umlClassDiagram.impl.UmlClassDiagramFactoryImpl;


public class testmain {

	public testmain() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 */
	
	static String readExpressionStatement(ExpressionStatement es)
	{
		String res="";
		
		
	
	Expression ii =es.getExpression();
	if(ii instanceof AssignmentImpl){
		AssignmentImpl res1=(AssignmentImpl) ii;
		FieldAccessImpl leftside=(FieldAccessImpl) res1.getLeftHandSide();
		SingleVariableAccessImpl rigAccessImpl=(SingleVariableAccessImpl) res1.getRightHandSide();
	String lef=leftside.getField().getVariable().getName();
	String r=rigAccessImpl.getVariable().getName();
		res= lef+res1.getOperator()+r+";";
		//System.out.println(lef+res1.getOperator()+r);
	}
	return res;
	}
	static String readReturnStatement(ReturnStatement es)
	{
		SingleVariableAccessImpl  ii =(SingleVariableAccessImpl) es.getExpression();
	    return "return "	+ii.getVariable().getName()+";";
		
		
			
	}
		
	
		
		
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
       /*ModiscoJavaModelLoader mod =*/ModiscoJavaModelLoader.getInstance();
      
      
   
       Model model= /*mod*/ModiscoJavaModelLoader.loadModiscoJavaModel("Product1Bank_java.xmi");
    
       Iterator<Package> packages = model.getOwnedElements().iterator();
       while (packages.hasNext()) {

           PackageImpl p = (PackageImpl) packages.next();
           System.out.println("package  "+p.getName()+";");
           
          
         if(!p.getName().equals("java"))
         {
        	 Iterator<AbstractTypeDeclaration> classs = p.getOwnedElements().iterator();
           while (classs.hasNext())
           {
        	   AbstractTypeDeclaration ab=classs.next();
        	   if(ab instanceof ClassDeclaration )
        	   {
        		   StringBuilder str=new StringBuilder();
        		   
        		   ClassDeclaration d=(ClassDeclaration) ab;
        		   str.append("package "+p.getName()+"\n");
        		   str.append(d.getModifier().getVisibility() +" class  "+d.getName() +"{\n");
        		  // System.out.println(d.getModifier().getVisibility() +"class  "+d.getName() +"{");  
        		EList<BodyDeclaration> bodys=d.getBodyDeclarations();
        		//read body declaration 
        		for(BodyDeclaration bo:bodys)
        		{
        			
        			if(bo instanceof FieldDeclarationImpl){
        				FieldDeclarationImpl fd=(FieldDeclarationImpl) bo;	
        			 str.append(fd.getModifier().getVisibility().getName()+" "+   fd.getType().getType().getName() +" " + fd.getFragments().get(0).getName()+";\n");
        				//  System.out.println(fd.getModifier().getVisibility().getName()+" "+   fd.getType().getType().getName() +" " + fd.getFragments().get(0).getName());
        			}
        				
        			else if (bo instanceof MethodDeclaration) {
        				 MethodDeclaration m= (MethodDeclaration) bo;
        				 
        		//read method declaration 
        			 String vis=m.getModifier().getVisibility().getName();
        		   String returntype=	m.getReturnType().getType().getName();
        	        EList<SingleVariableDeclaration> parmetres=  m.getParameters();
        	        String method=vis +" " +returntype +" "+m.getName()+"(";
        	        int i=0;
        	        if(parmetres.size()==0)
        	        {
        	        	method+=" )";
        	        }
        	        else
        	        {
        	        for(SingleVariableDeclaration var: parmetres)
        	        {i++;
        	        	if(i!=parmetres.size())
        	        	{
        	        		method+=var.getType().getType().getName() +" "+var.getName()+",";
        	        	}
        	        	else
        	        	{
        	        		method+=var.getType().getType().getName() +" "+var.getName()+")";
        	        	}
        	          
        	        }
        	        }
        	        str.append(method+"\n {");
        	        
        	        
        	     //   System.out.println(method);
        			
        	    	//end read method declaration 
        	        
        	        //read methode body 
        	        
        			org.eclipse.gmt.modisco.java.Block b=m.getBody();
        			 //if(m.getName().equals("deposit"))
            		 {
        			 EList<Statement>	sts=b.getStatements();
        			 for(Statement st:sts)
        			 {
        			
        				 if(st instanceof ExpressionStatement)
        				 {ExpressionStatementImpl es=(ExpressionStatementImpl) st;
        					 str.append(readExpressionStatement(es));
        				 }
        				 
        				 if(st instanceof ReturnStatement)
        				 {
        					
        					 
        				 }
        				
        				if(st instanceof IfStatement)
        				{
        					
        				}
        				if(st instanceof ForStatement)
        				{
        					
        				}
        				if(st instanceof WhileStatement)
        				{
        					
        				}
        				
        				
        			 }
					}//end read methode body
            		 if(returntype.equals("void"))
         	        {
         	        str.append("}\n");	
         	        }
         	        else
         	        {
         	        	str.append("return null;}\n");
         	        }
        			}
        			

        		}//end read body declaration
        		
        		str.append("//// end of class \n } \n");
        	System.err.println(str.toString());
        	   
           
         }
       

       }
      
	}
		
       }
       
	}}
